package com.lineage.server.datatables.sql;

import com.lineage.DatabaseFactory;
import com.lineage.server.datatables.CharObjidTable;
import com.lineage.server.datatables.storage.CharOtherStorage3;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.templates.L1PcOther3;
import com.lineage.server.utils.PerformanceTimer;
import com.lineage.server.utils.SQLUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class CharOtherTable3 implements CharOtherStorage3 {
  private static final Log _log = LogFactory.getLog(CharOtherTable3.class);
  
  private static final Map<Integer, L1PcOther3> _otherMap = new HashMap<>();
  
  public void load() {
    PerformanceTimer timer = new PerformanceTimer();
    Connection cn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
      cn = DatabaseFactory.get().getConnection();
      ps = cn.prepareStatement("SELECT * FROM `character_quest_map`");
      rs = ps.executeQuery();
      while (rs.next()) {
        int char_obj_id = rs.getInt("char_obj_id");
        if (CharObjidTable.get().isChar(char_obj_id) != null) {
          int type1 = rs.getInt("type1");
          int type2 = rs.getInt("type2");
          int type3 = rs.getInt("type3");
          int type4 = rs.getInt("type4");
          int type5 = rs.getInt("type5");
          int type6 = rs.getInt("type6");
          L1PcOther3 other = new L1PcOther3();
          other.set_objid(char_obj_id);
          other.set_type1(type1);
          other.set_type2(type2);
          other.set_type3(type3);
          other.set_type4(type4);
          other.set_type5(type5);
          other.set_type6(type6);
          addMap(char_obj_id, other);
          continue;
        } 
        delete(char_obj_id);
      } 
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(ps);
      SQLUtil.close(cn);
    } 
    _log.info("载入额外纪录资料数量: " + _otherMap.size() + "(" + timer.get() + "ms)");
  }
  
  private static void delete(int objid) {
    Connection cn = null;
    PreparedStatement ps = null;
    try {
      cn = DatabaseFactory.get().getConnection();
      ps = cn.prepareStatement("DELETE FROM `character_quest_map` WHERE `char_obj_id`=?");
      ps.setInt(1, objid);
      ps.execute();
      _otherMap.remove(Integer.valueOf(objid));
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(ps);
      SQLUtil.close(cn);
    } 
  }
  
  private static void addMap(int objId, L1PcOther3 other) {
    L1PcOther3 otherTmp = _otherMap.get(Integer.valueOf(objId));
    if (otherTmp == null)
      _otherMap.put(Integer.valueOf(objId), other); 
  }
  
  public L1PcOther3 getOther(L1PcInstance pc) {
    L1PcOther3 otherTmp = _otherMap.get(Integer.valueOf(pc.getId()));
    return otherTmp;
  }
  
  public void storeOther(int objId, L1PcOther3 other) {
    L1PcOther3 otherTmp = _otherMap.get(Integer.valueOf(objId));
    if (otherTmp == null) {
      addMap(objId, other);
      addNewOther(other);
    } else {
      updateOther(other);
    } 
  }
  
  private void updateOther(L1PcOther3 other) {
    Connection cn = null;
    PreparedStatement ps = null;
    try {
      int type1 = other.get_type1();
      int type2 = other.get_type2();
      int type3 = other.get_type3();
      int type4 = other.get_type4();
      int type5 = other.get_type5();
      int type6 = other.get_type6();
      cn = DatabaseFactory.get().getConnection();
      ps = cn.prepareStatement(
          "UPDATE `character_quest_map` SET `type1`=?,`type2`=?,`type3`=?,`type4`=?,`type5`=?,`type6`=? WHERE `char_obj_id`=?");
      int i = 0;
      ps.setInt(++i, type1);
      ps.setInt(++i, type2);
      ps.setInt(++i, type3);
      ps.setInt(++i, type4);
      ps.setInt(++i, type5);
      ps.setInt(++i, type6);
      ps.setInt(++i, other.get_objid());
      ps.execute();
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(ps);
      SQLUtil.close(cn);
    } 
  }
  
  private void addNewOther(L1PcOther3 other) {
    Connection cn = null;
    PreparedStatement ps = null;
    try {
      int oid = other.get_objid();
      int type1 = other.get_type1();
      int type2 = other.get_type2();
      int type3 = other.get_type3();
      int type4 = other.get_type4();
      int type5 = other.get_type5();
      int type6 = other.get_type6();
      cn = DatabaseFactory.get().getConnection();
      ps = cn.prepareStatement(
          "INSERT INTO `character_quest_map` SET `char_obj_id`=?,`type1`=?,`type2`=?,`type3`=?,`type4`=?,`type5`=?,`type6`=?");
      int i = 0;
      ps.setInt(++i, oid);
      ps.setInt(++i, type1);
      ps.setInt(++i, type2);
      ps.setInt(++i, type3);
      ps.setInt(++i, type4);
      ps.setInt(++i, type5);
      ps.setInt(++i, type6);
      ps.execute();
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(ps);
      SQLUtil.close(cn);
    } 
  }
  
  public void tam() {
    Connection cn = null;
    PreparedStatement ps = null;
    try {
      Iterator<L1PcOther3> iterator = _otherMap.values().iterator();
      L1PcOther3 l1PcOther3 = null;
      while (iterator.hasNext())
         l1PcOther3 = iterator.next(); 
      cn = DatabaseFactory.get().getConnection();
      ps = cn.prepareStatement("UPDATE `character_quest_map` SET `killCount`='0' AND `deathCount`='0'");
      ps.execute();
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(ps);
      SQLUtil.close(cn);
    } 
  }
}
